﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wicresoft.Shell.Data;
using Wicresoft.Shell.Logic;
using System.Data.Objects;
using System.Collections;

namespace TestProject
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class DatabaseUnitTest
    {
        public DatabaseUnitTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        public void DBTest()
        {
            TestContext.WriteLine("DBTest started");

            using (mobilegov_wcEntities database = new mobilegov_wcEntities())
            {
                TestContext.WriteLine("DBTest INSERT");
                var insertResult = new version();
                insertResult.VERSION_NAME = "Test Version";
                insertResult.VERSION_NUM = "10.0";
                insertResult.VERSION_PUBLISH_DATE = DateTime.Now;
                insertResult.VERSION_PUBLISH_STATUS = "9";
                insertResult.CLIENT_SYS_TYPE = "VM";

                database.AddToversion(insertResult);
                database.SaveChanges();

                TestContext.WriteLine("DBTest SELECT");
                var result = from p in database.version
                             where p.VERSION_NAME == "Test Version"
                             select p;

                foreach (var row in result)
                {
                    TestContext.WriteLine(row.VERSION_NAME);
                }

                if (result.Count() == 0)
                {
                    throw new Exception("No data in table Version");
                }

                TestContext.WriteLine("DBTest Update");
                foreach (var row in result)
                {
                    row.VERSION_NAME = "Test Version 2";
                }
                database.SaveChanges();

                TestContext.WriteLine("DBTest SELECT");
                result = from p in database.version
                         where p.VERSION_NAME == "Test Version 2"
                         select p;

                foreach (var row in result)
                {
                    TestContext.WriteLine(row.VERSION_NAME);
                }

                if (result.Count() == 0)
                {
                    throw new Exception("No data in table Version");
                }

                TestContext.WriteLine("DBTest Delete");
                foreach (var row in result)
                {
                    database.DeleteObject(row);
                }

                database.SaveChanges();
            }

            TestContext.WriteLine("DBTest ended");
        }
        public void DBTableTest()
        {
            TestContext.WriteLine("DBTest started");

            DatabaseLogic database = new DatabaseLogic();

            IEnumerable<ApplicationTable> applications = database.GetApplicationTableData();

            IEnumerable<FunctionTable> functions = database.GetFunctionTableData();

            foreach (var row in applications)
            {
                TestContext.WriteLine(row.APP_ID.ToString());
                TestContext.WriteLine(row.APP_FATHER_ID.ToString());
                TestContext.WriteLine(row.APP_NAME);
                TestContext.WriteLine(row.APP_CODE);
                TestContext.WriteLine(row.APP_DESC);
            }

            foreach (var row in functions)
            {
                TestContext.WriteLine(row.APP_ID.ToString());
                TestContext.WriteLine(row.FUNCTION_ID.ToString());
                TestContext.WriteLine(row.FUNCTION_NAME);
                TestContext.WriteLine(row.FUNCTION_CODE);

                if (row.FUNCTION_DESC != null)
                {
                    TestContext.WriteLine(row.FUNCTION_DESC);
                }
            }


            TestContext.WriteLine("DBTest ended");
        }

        //[TestMethod]
        //public void DBInsertUpdateTest()
        //{
        //    TestContext.WriteLine("DBTest started");

        //    DatabaseLogic database = new DatabaseLogic();
        //    database.CutApplicationNode(71, 59);
        //    int lastIndex = database.CopyApplicationNode(71, 70);

        //    TestContext.WriteLine(lastIndex.ToString());

        //    using (mobilegov_wcEntities db = new mobilegov_wcEntities())
        //    {
        //        var delResult = from p in db.application
        //                        where p.APP_ID == lastIndex
        //                        select p;

        //        foreach (var row in delResult)
        //        {
        //            db.DeleteObject(row);
        //        }
        //        db.SaveChanges();
        //    }

        //    TestContext.WriteLine("DBTest ended");
        //}


        //[TestMethod]
        //public void DBCopyNode()
        //{
        //    TestContext.WriteLine("DBCopyNode started");

        //    DatabaseLogic database = new DatabaseLogic();
        //    int lastIndex = database.CopyApplicationNode(80, 80);

        //    TestContext.WriteLine(lastIndex.ToString());

        //    using (mobilegov_wcEntities db = new mobilegov_wcEntities())
        //    {
        //        TestContext.WriteLine("AppId={0}, application count={1}, function count={2}", lastIndex,
        //            db.application.
        //            Where(p => p.application2.APP_ID == lastIndex).Count(),
        //            db.function.
        //            Where(p => p.application.APP_ID == lastIndex).Count());
        //    }


        //    TestContext.WriteLine("DBCopyNode ended");
        //}
    }
}
